﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Reflection;
using System.Windows.Forms;


using Microsoft.VisualStudio.CommandBars;
using EnvDTE;
using EnvDTE80;
using EnvDTE90;

namespace iMainAddin
{
    public abstract class iCommand
    {
        static private DTE m_Application = null;
        private System.Guid id = System.Guid.NewGuid();
        private string m_Caption = null;
        private string m_ButtonName = null;
        private string m_TooltipText = null;
        protected System.Configuration.Configuration appConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
        private Command m_Command;

        static public void Startup(DTE application)
        {
            m_Application = application;
        }

        #region Property
        static protected DTE Application
        {
            get { return m_Application; }
            set
            {
                if (m_Application == null)
                    m_Application = value;
            }
        }

        public System.Guid Id
        {
            get
            {
                return id;
            }
        }

        public string Name
        {
            get { return m_ButtonName; }
            set { m_ButtonName = value; }
        }

        public string Caption
        {
            get
            {
                return m_Caption;
            }
            set
            {
                m_Caption = value;
            }
        }

        public string TooltipText
        {
            get
            {
                return m_TooltipText;
            }
            set
            {
                m_TooltipText = value;
            }
        }

        public Command VSCommand
        {
            get { return m_Command; }
            set { m_Command = value; }
        }
        #endregion

        public iCommand(string commandType, string tooltipText)
        {
            this.m_Caption = commandType;
            this.m_ButtonName = commandType;
            this.m_TooltipText = tooltipText;
        }

        public CommandBar GetCommandBar(string sCommandBarName)
        {
            return ((CommandBars)m_Application.DTE.CommandBars)[sCommandBarName];
        }
        public Command GetVSCommand( AddIn addIninst)
        {
            Commands2 commands = (Commands2)m_Application.Commands;
            Command command = null;


            IEnumerator commandsRator = commands.GetEnumerator();
            while (commandsRator.MoveNext())
            {
                command = (Command)commandsRator.Current;
                if (command.Name.Contains(Caption))
                    return command;
            }



            try
            {
                object[] contextGUIDS = new object[] { };
                VSCommand = commands.AddNamedCommand2(addIninst, Name, Caption, TooltipText, true, 59, ref contextGUIDS
                    , (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled
                    , (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
            }
            catch (Exception e)
            {
                MessageBox.Show("Failed CreateCommandItem - " + e.Message);
            }
            return command;
        }
        public abstract void Perform();
    }
}
